package com.example.service.wechat.impl;

import com.example.mapper.web.StopMapper;
import com.example.pojo.web.Bike;
import com.example.pojo.web.Stop;
import com.example.pojo.wechat.WxPoint;
import com.example.pojo.wechat.WxRegion;
import com.example.service.web.BikeService;
import com.example.service.wechat.WxRegionService;
import org.locationtech.jts.geom.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxRegionServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/17 - 04 - 17 - 16:13
 * @version: 1.0
 */
@Service
public class WxRegionServiceImpl implements WxRegionService {
    //properties
    @Autowired
    private StopMapper stopMapper;
    @Autowired
    private BikeService bikeService;

    //methods
    @Override
    public List<Bike> queryRegionForBikes(WxRegion region) {
        try {
            Envelope envelope = wxRegion2Envelope(region);
            Geometry geometry = envelope2Geometry(envelope);
            //获取停车点区域
            List<Stop> stops = stopMapper.selectStopsByName_Belong("", "");
            Map<String,Integer> intersection_stopId_map = new HashMap<>();
            stops.forEach(e->{
                if (e.getGeom()!=null&&e.getGeom().intersects(geometry)){
                    intersection_stopId_map.put("id",e.getId());
                }
            });
            System.out.println(intersection_stopId_map);
            //根据停车区id表,查询单车列表
            List<Bike> bikeList = bikeService.selectBikeByStopMap(intersection_stopId_map);
            bikeList.forEach(System.out::println);
            return bikeList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

//    @Override
//    public Bike queryBufferForSingleBike(WxPoint wxPoint) {
//        if (wxPoint==null)
//            return null;
//        Point point = wxPoint2Poin(wxPoint);
//        System.out.println(point);
//        //创建缓冲区
//        Geometry buffer = point.buffer(1);
//        //获取停车点区域
//        System.out.println(buffer);
//        //
//
//        return null;
//    }

    /**
     * WxPoint转为Point
     * @param wxPoint
     * @return
     */
    private Point wxPoint2Poin(WxPoint wxPoint){
        if (wxPoint==null)
            return null;
        GeometryFactory geometryFactory = new GeometryFactory();
        Coordinate coordinate = new Coordinate(wxPoint.getLongitude(),wxPoint.getLatitude());
        Point point = geometryFactory.createPoint(coordinate);
        return point;
    }


    /**
     * Envelope转Geomtery
     * @return
     */
    private Geometry envelope2Geometry(Envelope envelope){
        if (envelope==null)
            return null;
        GeometryFactory geometryFactory = new GeometryFactory();
        Geometry rectangle_geom = geometryFactory.toGeometry(envelope);
        return rectangle_geom;
    }


    /**
     * wxRegion转Envelope
     * @param region
     * @return
     */
    private Envelope wxRegion2Envelope(WxRegion region){
        if (region==null){
            return null;
        }
        WxPoint southwest = region.getSouthwest();
        Coordinate southwest_coord = new Coordinate(southwest.getLongitude(),southwest.getLatitude());
        WxPoint northeast = region.getNortheast();
        Coordinate northeast_coord = new Coordinate(northeast.getLongitude(),northeast.getLatitude());
        //创建Envelope
        Envelope envelope = new Envelope(southwest_coord,northeast_coord);
        return envelope;
    }

}
